package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.mongodb.client.model.Filters;
import com.weiqiao.mall.domain.Comment;
import com.weiqiao.mall.domain.CommodityEntity;
import com.weiqiao.mall.domain.ShopComment;
import com.weiqiao.mall.repository.ICommentRepository;
import com.weiqiao.mall.repository.ICommodityRepository;
import com.weiqiao.mall.service.ICommentService;
import com.weiqiao.utils.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("CommentServiceImpl")
public class CommentServiceImpl implements ICommentService {

    @Autowired
    @Qualifier("CommentRepositoryImpl")
    private ICommentRepository commentRepository;

    /**
     * 商品管理数据层
     */
    @Autowired
    private ICommodityRepository commodityRepository;

    @Override
    public List<ShopComment> getCommentList(int company_id, Document doc, Pageable pageable) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Gson gson = new Gson();
        List<ShopComment> proList = new ArrayList<>();
        List<String> list = commentRepository.findCommentPage(company_id, doc, pageable);
        if (!list.isEmpty()) {
            for (String a : list) {
                ShopComment pro = gson.fromJson(a, ShopComment.class);
                String commodity = commodityRepository.findCommodity(company_id, Filters.eq("commodity_id", pro.getCommodity_id()));
                if (!StringUtils.isBlank(commodity)) {
                    CommodityEntity entity = gson.fromJson(commodity, CommodityEntity.class);
                    pro.setName(entity.getName());
                    pro.setCommodity_url(entity.getCover_url());
                }
                String time = pro.getCreate_date();
                long create_time = Long.parseLong(time);
                pro.setCreate_date(sd.format(new Date(create_time)));
                proList.add(pro);
            }
        }
        return proList;
    }

    /**
     * 新增评论
     *
     * @param json
     * @param company_id
     * @return
     */
    @Override
    public boolean addCommentOne(String json, int company_id) {
        return commentRepository.addCommentOne(json, company_id);
    }

    /**
     * 新增多条评论
     *
     * @param documents
     * @param company_id
     * @return
     */
    @Override
    public boolean addCommentMany(List<Document> documents, int company_id) {
        return commentRepository.addCommentMany(documents, company_id);
    }

    /**
     * 修改一条评论
     *
     * @param filter
     * @param doc
     * @param flag
     * @param company_id
     * @return
     */
    @Override
    public boolean updateCommentOne(Bson filter, Document doc, boolean flag, int company_id) {
        return commentRepository.updateCommentOne(filter, doc, flag, company_id);
    }

    /**
     * 修改多条评论
     *
     * @param filter
     * @param doc
     * @param flag
     * @param company_id
     * @return
     */
    @Override
    public boolean updateCommentMany(Bson filter, Document doc, boolean flag, int company_id) {
        return commentRepository.updateCommentMany(filter, doc, flag, company_id);
    }

    /**
     * 根据条件查询一条评论
     *
     * @param filter
     * @param company_id
     * @return
     */
    @Override
    public Comment findCommentOne(Bson filter, int company_id) {
        String commentOne = commentRepository.findCommentOne(filter, company_id);
        if (!StringUtil.isBlank(commentOne)) {
            return new Gson().fromJson(commentOne, Comment.class);
        }
        return null;
    }

    /**
     * 根据条件查询多条评论
     *
     * @param filter
     * @param company_id
     * @return
     */
    @Override
    public List<Comment> findCommentMany(Bson filter, int company_id) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        List<Comment> commentList = new ArrayList<>();
        List<String> commentMany = commentRepository.findCommentMany(filter, company_id);
        if (commentMany != null && commentMany.size() > 0) {
            for (String s : commentMany) {
                Comment comment = new Gson().fromJson(s, Comment.class);
                String time = comment.getCreate_date();
                long create_time = Long.parseLong(time);
                comment.setCreate_date(sd.format(new Date(create_time)));
                commentList.add(comment);
            }
        }
        return commentList;
    }

    @Override
    public int getCount(int company_id, Document doc) {
        return commentRepository.getCount(company_id, doc);
    }


    /**
     * 删除一条评论
     *
     * @param filter
     * @param company_id
     * @return
     */
    @Override
    public boolean deleteCommentOne(Bson filter, int company_id) {
        return commentRepository.deleteCommentOne(filter, company_id);
    }

    /**
     * 删除多条评论
     *
     * @param filter
     * @param company_id
     * @return
     */
    @Override
    public boolean deleteCommentMany(Bson filter, int company_id) {
        return commentRepository.deleteCommentMany(filter, company_id);
    }

    @Override
    public List<Comment> findCommentManySortLimit(Bson filter, Bson sort, int limit, int company_id) {
        List<Comment> commentList = new ArrayList<>();
        List<String> commentMany = commentRepository.findCommentManySortLimit(filter, sort, limit, company_id);
        if (commentMany != null && commentMany.size() > 0) {
            for (String s : commentMany) {
                Comment comment = new Gson().fromJson(s, Comment.class);
                commentList.add(comment);
            }
        }
        return commentList;
    }
}
